package com.example.lin.day9_29;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class MyBehavior extends CoordinatorLayout.Behavior {

    public MyBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * 当 dependency 发生变化的时候,该方法被调用
     *
     * @param parent
     * @param child
     * @param dependency
     * @return
     */
    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
        return dependency instanceof AppBarLayout;//参照的View 是不是 AppBarLayout.如果是,则返回true
    }

    /**
     * 当 layoutDependsOn 返回 true的时候.该方法被调用
     *
     * @param parent
     * @param child      配置该 Behavior 的View
     * @param dependency 参照的View
     * @return
     */
    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {

        int height = dependency.getHeight();
        int max = height / 2;

        float y = dependency.getY() - 48;
        float cy = -y;

        float v = cy / max;
        if (v <= 1) {

            child.setScaleX(1 - v * 0.5f);
            child.setScaleY(1 - v * 0.5f);

            child.setTranslationX(v * -280);

        }
        return true;
    }


}


